/*
 * @Author: 王雁鹏
 * @Date: 2023-05-30 09:59:21
 * @LastEditTime: 2023-07-31 17:03:29
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \expressf:\web-vue\bs-nodejs-background-code\utils\func.js
 * ------------------
 */
const jwt = require("jsonwebtoken")
//撒盐，加密时候混淆
const secret = '113Bmongojsdalkfnxcvmas'

//生成token
//info也就是payload是需要存入token的信息
function createToken(info) {
    let token = jwt.sign(info, secret, {
        //Token有效时间 单位s
        expiresIn: 60 * 60 * 10
    })
    return token
}

//验证Token
function verifyToken(token) {
    return new Promise((resolve, reject) => {
        jwt.verify(token, secret, (error, result) => {
            if (error) {
                reject(error)
            } else {
                resolve(result)
            }
        })
    })
}

function generateUUID() {
    // 将当前时间戳转换为16进制字符串，以便在生成UUID时使用  
    let timestamp = Math.round(+new Date()).toString(16);

    // 将时间戳转换为UUID格式  
    let uuid = timestamp.replace(/-/g, '')
        .replace(/_/g, '')
        .replace(/\d/g, (() => {
            let random = Math.floor(Math.random() * 16);
            return random.toString(16);
        }));

    return uuid;
}

// 使用生成的UUID  
console.log(generateUUID()); // 输出：5eb63bb-4a91-42a5-9d26-00c04fc902d8

function generateId() {
    // 将当前时间戳转换为16进制字符串，以便在生成UUID时使用  
    let timestamp = Math.round(+new Date()).toString(16);

    // 将时间戳转换为UUID格式，并确保生成的ID是唯一的  
    let id = timestamp.replace(/-/g, '')
        .replace(/_/g, '')
        .replace(/\d/g, (() => {
            let random = Math.floor(Math.random() * 16);
            return random.toString(16);
        }));

    // 使用生成的ID  
    console.log(id); // 输出：123456789  
}

// 订单号生成函数
function randomNumber() {
    const now = new Date()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let hour = now.getHours()
    let minutes = now.getMinutes()
    let seconds = now.getSeconds()
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    hour = hour < 10 ? "0" + hour : hour;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();
    return orderCode;
}

function createTime(params = {}) {
    let { minutes, time } = params
    const date = time ? new Date(time) : new Date();
    if (minutes) {
        date.setMinutes(date.getMinutes() + minutes)
    }
    console.log(date, "datedate")
    const year = date.getFullYear().toString().padStart(4, '0');
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}` // 2023-02-18 21:49:05
}

function errorCode(key) {
    let obj = {
        100: { code: 100, msg: "用户未登录！" }
    }
    return obj[key]
}
let filterBool = (path, data) => {
    return { bool: true }
    let openid = data.openid
    let arr = [
        "/getAddress",
        "/pay",
        "/setShop",
        "/shop"
    ]
    let len = arr.filter(item => {
        return path == item
    }).length
    if (len == 0) {
        return { bool: true }
    }
    if (openid) {
        return { bool: true }
    } else {
        return { bool: false, code: errorCode(100) }
    }
}
// 使用生成的ID  
console.log(generateId()); // 输出：123456789
module.exports = { generateUUID, errorCode, filterBool, generateId, createTime, createToken, randomNumber, verifyToken }